package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

public class UserMammonHistoryDaoImpl extends TempDao {

    public int getUserMammonHistoryDecoration(long userId, List<Integer> goodList, Date date) {
        ResultSet rs = null;
        CallableStatement call = null;
        String goodsInfo = "(";
        for (int i = 0; i < goodList.size(); i++) {
            if (i == goodList.size() - 1) {
                goodsInfo += goodList.get(i) + ")";
            } else {
                goodsInfo += goodList.get(i) + ",";
            }
        }
        String getUserMammonHistoryDecorationSql = "select count(1) as result from `t_l_decoration` where `userId`=? and `time`>=? and decorationId in "+goodsInfo;
        try {
            getDb().setConnName(DbNameStrategy.getLogDb());
            call = getDb().getConnection().prepareCall(getUserMammonHistoryDecorationSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setString(i++, DateHelper.date2String(date));
            rs = call.executeQuery();
            if (rs.next()) {
                return rs.getInt("result");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return -1;
    }


}
